##################################################################
# File : lua/sim/unit.lua
# Description : Unit lua module
# Author : GPG
##################################################################

local Entity = import('/lua/sim/Entity.lua').Entity

Unit = Class(moho.unit_methods, Entity) {

    # We already have a C++ object, don't call Entity's version.
    __init = function(self,spec) end,
    __post_init = function(self,spec) end,

    # Engine Events
    OnPreCreate = function(self)
        self:InitSyncTable()

        if not self.Trash then
            self.Trash = TrashBag()
        end

        if not self.TrashOnKilled then
            self.TrashOnKilled = TrashBag()
        end
    end,

    KillSelf = function(self)
        self:Kill()
    end,

    OnKilled = function(self)
        if self.TrashOnKilled then
            self.TrashOnKilled:Destroy()
        end
    end,

    OnDestroy = function(self)
        Entity.OnDestroy(self)
        if self.Trash then
            self.Trash:Destroy()
            self.Trash = nil
        end
        if self.TrashOnKilled then
            self.TrashOnKilled:Destroy()
            self.TrashOnKilled = nil
        end
    end,

    OnInvalidPosition = function(self)
        self:KillSelf()
    end,

    OnCreate = function(self) end,
    OnStartBeingBuilt = function(self,creator,layer) end,
    OnStopBeingBuilt = function(self,creator,layer) end,
    OnPaused = function(self) end,
    OnUnpaused = function(self) end,
    OnStartRepeatQueue = function(self) end,
    OnStopRepeatQueue = function(self) end,
    OnAssignedFocusEntity = function(self) end,
    CheckCanBeKilled = function(self) return true end,
    OnDecayed = function(self) end,
    OnTerrainTypeChange = function(self,new,old) end,
    OnAdjacentTo = function(self, adjacentUnit, triggerUnit) end,
    OnNotAdjacentTo = function(self, adjacentUnit) end,
    OnConsumptionActive = function(self) end,
    OnConsumptionInActive = function(self) end,
    OnProductionActive = function(self) end,
    OnProductionInActive = function(self) end,
    OnMotionHorzEventChange = function( self, new, old ) end,
    OnStunned = function(self) end,
    OnHasEconomyToBuild = function(self,buildBp) end,
    OnFalling = function(self) end,

    # Core unit methods
    ForkThread = function(self, fn, ...)
        if fn then
            local thread = ForkThread(fn, self, unpack(arg))
            self.Trash:Add(thread)
            return thread
        else
            return nil
        end
    end,
}